


# import json
# import time
# from selenium.webdriver.common.by import By
# from selenium import webdriver

# driver = webdriver.Chrome()
# driver.implicitly_wait(10)

# # 读取 user.json 文件中的账号密码信息
# with open('user.json', 'r') as f:
#     user_data = json.load(f)

# cookies_data = []

# for user in user_data:
#     driver.get('https://steamcommunity.com/login/home/?goto=')

#     # 找到账号输入框并输入账号
#     username_input = driver.find_element(By.XPATH, '//*[@id="responsive_page_template_content"]/div[1]/div[1]/div/div/div/div[2]/div/form/div[1]/input')
#     username_input.send_keys(user["username"])

#     # 找到密码输入框并输入密码
#     password_input = driver.find_element(By.XPATH, '//*[@id="responsive_page_template_content"]/div[1]/div[1]/div/div/div/div[2]/div/form/div[2]/input')
#     password_input.send_keys(user["pwd"])

#     # 找到登录按钮并点击
#     login_button = driver.find_element(By.XPATH, '//*[@id="responsive_page_template_content"]/div[1]/div[1]/div/div/div/div[2]/div/form/div[4]/button')
#     login_button.click()

#     time.sleep(5)  # 等待页面加载

#     # 获取所有 cookies
#     cookies = driver.get_cookies()

#     # 提取需要的两个 cookie 值
#     sessionid_value = ""
#     steamLoginSecure_value = ""
#     for cookie in cookies:
#         if cookie["name"] == "sessionid":
#             sessionid_value = cookie["value"]
#         elif cookie["name"] == "steamLoginSecure":
#             steamLoginSecure_value = cookie["value"]

#     # 构建要保存的 JSON 数据格式
#     cookies_data.append({
#         "username": user["username"],
#         "sessionid": sessionid_value,
#         "steamLoginSecure": steamLoginSecure_value
#     })

# # 写入本地 userCookies.json 文件
# with open('userCookies.json', 'w') as f:
#     json.dump(cookies_data, f)



import json
import time
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common.by import By
from selenium import webdriver

# 设置 Chrome 浏览器为无痕模式
options = ChromeOptions()
options.add_argument("--incognito")

driver = webdriver.Chrome(options=options)
driver.implicitly_wait(10)

# 读取 user.json 文件中的账号密码信息
with open('user.json', 'r') as f:
    user_data = json.load(f)

cookies_data = []

for user in user_data:
    driver.get('https://steamcommunity.com/login/home/?goto=')

    # 找到账号输入框并输入账号
    username_input = driver.find_element(By.XPATH, '//*[@id="responsive_page_template_content"]/div[1]/div[1]/div/div/div/div[2]/div/form/div[1]/input')
    username_input.send_keys(user["username"])

    # 找到密码输入框并输入密码
    password_input = driver.find_element(By.XPATH, '//*[@id="responsive_page_template_content"]/div[1]/div[1]/div/div/div/div[2]/div/form/div[2]/input')
    password_input.send_keys(user["pwd"])

    # 找到登录按钮并点击
    login_button = driver.find_element(By.XPATH, '//*[@id="responsive_page_template_content"]/div[1]/div[1]/div/div/div/div[2]/div/form/div[4]/button')
    login_button.click()

    time.sleep(5)  # 等待页面加载

    # 获取所有 cookies
    cookies = driver.get_cookies()

    # 提取需要的两个 cookie 值
    sessionid_value = ""
    steamLoginSecure_value = ""
    for cookie in cookies:
        if cookie["name"] == "sessionid":
            sessionid_value = cookie["value"]
        elif cookie["name"] == "steamLoginSecure":
            steamLoginSecure_value = cookie["value"]

    # 构建要保存的 JSON 数据格式
    cookies_data.append({
        "username": user["username"],
        "sessionid": sessionid_value,
        "steamLoginSecure": steamLoginSecure_value
    })

    # 一个账号处理完成后关闭浏览器
    driver.quit()

    # 重新打开一个无痕模式的浏览器
    driver = webdriver.Chrome(options=options)
    driver.implicitly_wait(10)

# 写入本地 userCookies.json 文件
with open('userCookies.json', 'w') as f:
    json.dump(cookies_data, f)



# 打开目标网站


# 关闭浏览器
# driver.quit()
# 这里我们什么也不做，只是让脚本保持运行
try:
    # 使用无限循环来保持程序运行
    while True:
        # 可以在这里添加一些检查或处理逻辑，但现在我们只是简单地等待
        pass
except KeyboardInterrupt:
    # 当用户按下Ctrl+C时，会触发KeyboardInterrupt异常
    # 这允许用户通过标准方式优雅地退出程序
    print("程序被用户中断，正在关闭浏览器...")
    driver.quit()
    print("浏览器已关闭。")